//
//  MainTabBar.swift
//  SinaWeibo
//
//  Created by NeXTSTEP on 15/9/29.
//  Copyright © 2015年 NeXTSTEP. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    
    //懒加载得到一个按钮
    lazy var composeBtn: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        self.addSubview(btn)
        return btn
    }()

    override func layoutSubviews() {
        
        super.layoutSubviews()
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let frame = CGRect(x: 0, y: 0, width: w , height: h)
        
        var indxe:CGFloat = 0
        for view in subviews {
            if view is UIControl && !(view is UIButton) {
                view.frame = CGRectOffset(frame, indxe * w, 0)
//                if indxe == 1 {
//                    indxe++
//                }
//                indxe++
                
                indxe += (indxe == 1 ? 2 : 1)
            }
        }
        
        composeBtn.frame = CGRectOffset(frame, 2 * w, 0)
       
    }
}
